﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using KoalaGIS.Tools.BaiduDoc;

namespace BDOWN
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            this.progressDownLoad.Visible = true;

            try
            {
                KoalaGIS.Tools.BaiduDoc.BaiduDocDownload down = new KoalaGIS.Tools.BaiduDoc.BaiduDocDownload();
                //http://wenku.baidu.com/view/e6698789680203d8ce2f248b.html
                string url = this.txtDocUrl.Text;
                //BaiduDocInfo info = down.GetDocInfo(url);

                down.SwfDownLoadEvent += new DownLoadingHandler(down_SwfDownLoadEvent);
                down.Jpg2PdfEvent += new DownLoadingHandler(down_Jpg2PdfEvent);

                //down.Process(info, "D:\\BaiduDown\\");

                down.DownLoadPdf(url);

                //this.progressDownLoad.Minimum = 0;
                //this.progressDownLoad.Maximum = 20;


                //down.jpg2pdf("D:\\BaiduDown\\", "D:\\BaiduDown\\test.pdf");
                this.lblProgress.Text = "恭喜您！下载完成！";
                MessageBox.Show("下载完成！");
            }
            catch (Exception er)
            {
                if (er.Message == "doc以外的文档还没实现")
                {
                    MessageBox.Show("该类型文档的下载功能没有添加，请将该网址发给作者，马上给你弄出来！");
                }
                this.lblProgress.Text = "对不起，下载失败！";
            }
        }

        void down_Jpg2PdfEvent(int total, int curnum)
        {
            Application.DoEvents();
            this.lblProgress.Text = "生成pdf中....." + (curnum + 1) + "/" + total;
            if (this.progressDownLoad.Maximum != total)
            {
                this.progressDownLoad.Maximum = total;
                this.progressDownLoad.Step = 1;
            }
            this.progressDownLoad.Value = curnum+1;
            
        }

        void down_SwfDownLoadEvent(int total, int curnum)
        {
            Application.DoEvents();
            this.lblProgress.Text = "下载资源中....." +( curnum + 1) + "/" + total;
            if (this.progressDownLoad.Maximum != total)
            {
                this.progressDownLoad.Maximum = total;
                this.progressDownLoad.Step = 1;
            }
            this.progressDownLoad.Value = curnum + 1;

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("您确定要退出程序？", "退出程序", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                this.Close();
                Application.Exit();
            }
        }

        private void btnOpenFolder_Click(object sender, EventArgs e)
        {
            MessageBox.Show("@_@ 正在实现中....");
        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            AboutBox dlg = new AboutBox();
            dlg.ShowDialog();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {

        }
    }
}
